package huoye.shops.huoye.mine.activity;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.utils.StringUtils;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseActivity;
import huoye.shops.base.BaseTakePhotoActivity;
import huoye.shops.base.UserManager;
import huoye.shops.common.model.OssImageData;
import huoye.shops.huoye.mine.model.QrCodeInfo;
import huoye.shops.huoye.mine.presenter.ServiceSettingPresenter;
import huoye.shops.huoye.mine.view.IServiceSettingView;

public class ServiceSettingActivity extends BaseTakePhotoActivity implements IServiceSettingView{

    @BindView(R.id.service_setting_et_tel)
    EditText etTel;

    @BindView(R.id.service_setting_iv_del_pic_logo)
    ImageView ivDelPicLogo;

    @BindView(R.id.service_setting_iv_add_pic_logo)
    ImageView ivAddPicLogo;
    private ServiceSettingPresenter mServiceSettingPresenter;


    @OnClick(R.id.service_setting_iv_add_pic_logo)
    void onAddPicLogoClick(){
        takePhoto();
    }

    @OnClick(R.id.service_setting_iv_del_pic_logo)
    void onDelPicLogoClick(){
        mServiceSettingPresenter.deleteOssImg(imgPath);
    }

    @OnClick(R.id.service_setting_btn_save_message)
    void onSaveMessageClick(){
        String tel = etTel.getText().toString();
        mServiceSettingPresenter.saveServiceSetting(tel,imgPath);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_service_setting;
    }




    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("客服设置");
        mServiceSettingPresenter = new ServiceSettingPresenter();
        mServiceSettingPresenter.addTaskListener(this);
        mServiceSettingPresenter.onStart();
        ivDelPicLogo.setVisibility(View.GONE);
        mServiceSettingPresenter.getShopQrCodeInfo();
    }


    private String imgPath = "";
    @Override
    public void takeSuccess(TResult result) {
        if (result != null) {
            TImage image = result.getImage();
            imgPath = image.getCompressPath();
            String userShopId = UserManager.getInstance().getUserShopId();
            mServiceSettingPresenter.uploadOssImg(userShopId,"shop",imgPath);

            Glide.with(this)
                    .load(new File(imgPath))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .into(ivAddPicLogo);
            ivDelPicLogo.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void setUpload(OssImageData ossImageData) {
        imgPath = ossImageData.getFilePath();
    }

    @Override
    public void delUpload() {
        ivAddPicLogo.setImageResource(R.mipmap.ic_add_image);
        ivDelPicLogo.setVisibility(View.GONE);
        imgPath = "";
    }

    @Override
    public void qrCodeInfo(QrCodeInfo qrCodeInfo) {
        if(StringUtils.isNotEmpty(qrCodeInfo.getImgUrl())){
            etTel.setText(qrCodeInfo.getMobile());
            etTel.setSelection(etTel.getText().length());//将光标移至文字末尾
            Glide.with(this)
                    .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+qrCodeInfo.getImgUrl())
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_error)
                    .error(R.mipmap.ic_error)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(ivAddPicLogo);
            ivDelPicLogo.setVisibility(View.VISIBLE);
            imgPath = qrCodeInfo.getImgUrl();
        }

    }
}
